/**
 * 权限声明：
 */
package com.handy.webwork.action.result;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.webwork.action.AbstractAction;
import com.handy.webwork.action.ActionMapping;
import com.handy.webwork.config.HandyConfiguration;
import com.handy.webwork.view.MessageResource;


/**
 * 默认视图输出，jsp,html,htm
 * 
 * @author rocken.zeng@gmail.com
 * 
 */
public class Default {
	private static Log log = LogFactory.getLog(Default.class);

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context;

	public Default(HttpServletRequest request, HttpServletResponse response,ServletContext context) {
		this.request = request;
		this.response = response;
		this.context = context;
		if (log.isDebugEnabled()){
			log.info("视图：默认的视图输出。");
		}
	}

	/**
	 * 当result类型为空时，跳转到下个指定的页面。request范围内的数据共享。
	 * 
	 * @param action
	 * @param resultValue
	 * @throws IOException
	 * @throws ServletException 
	 */
	@SuppressWarnings("unchecked")
	public void doDefault(ActionMapping actionMapping,AbstractAction action, String resultValue){
		if (null == resultValue || "".equals(resultValue))
			return;
		//当没有指定result类型时让容器处理视图。
		String url = resultValue;
		if (!url.startsWith("/")) {
			url = "/" + url.trim();
		}
		//将bean值放入request中
		HashMap root = new HashMap();
		if (HandyConfiguration.autoHandleParam()) {// 自动将action的get,set字段装载到root中，在视图中显示。
			try {
				if (null!=PropertyUtils.describe(action))
					root.putAll(PropertyUtils.describe(action));
			} catch (IllegalAccessException e) {
				log.error(e.getMessage());
			} catch (InvocationTargetException e) {
				log.error(e.getMessage());
			} catch (NoSuchMethodException e) {
				log.error(e.getMessage());
			}
		} else {
			HashMap _root = action.sendData();
			if (null != _root) {
				root.putAll(_root);
			}
		}
		request.setAttribute("action", MessageResource.getInstance());
		request.setAttribute("root", root);
		Set<Entry> params = root.entrySet();
		for (Map.Entry entry : params) {
			String _key = (String) entry.getKey();
			request.setAttribute(_key, root.get(_key));
			//log.info(_key+"="+root.get(_key));
		}
		if (null == actionMapping.getPluginName()){
			request.setAttribute("plugin", "");
		}else{
			request.setAttribute("plugin", actionMapping.getPluginName());
		}
		if (log.isDebugEnabled()){
			log.debug("forward to : " +url);
		}
		RequestDispatcher rd = context.getRequestDispatcher(url);
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			log.error(e.getMessage());
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
}
